const app = getApp();
var QQMapWX = require('../../resource/js/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data:{
    tabbar:{},
    showView:true,
    StatusBar:app.globalData.StatusBar,
    CustomBar:app.globalData.CustomBar,
    cardCur:0,
    grids: [0, 1, 2, 3, 4, 5, 6, 7],
    imgUrls:[],
    indicatorDots:true,//是否显示面板指示点
    autoplay:true,//是否自动切换
    interval:3000,//自动切换时间间隔
    duration:1000,//滑动动画时长
    inputShowed:false,
    inputVal:"",
    address:'',
    login: false,
    template_zhuan: []//赚赏金组件
  },
  map:function(){
    wx.navigateTo({
      url: '../map/map'
    })
  },
  coupon:function(){
    wx.navigateTo({
      url: '../coupon/coupon?type=1'
    })
  },
  change:function(){
    const that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  close:function(){
    const that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  onLoad(){
    const that = this;
    const value = wx.getStorageSync('localhost');
    app.changeTabBar();
    app.util.request({
      url: 'Index/Index/NavList', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res){
        that.setData({
          grids:res.data
        })
      }
    })
    app.util.request({
      url: 'Index/Index/SildeList',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        var data = res.data;
        var image = new Array();
        for (var i = 0; i < data.length;i++){
          image[i] = data[i]['image'];
        }
        if (data.length < 2){
          that.setData({
            indicatorDots:false
          })
        }
        that.setData({
          imgUrls: image
        })
      }
    })
    qqmapsdk = new QQMapWX({key:'KJHBZ-PMX6X-MXE44-TRKVY-LC6YQ-JYBTM'});
    if(value){
      that.qqmap(value.latitude + ',' + value.longitude);
    }else{
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          that.qqmap(latitude + ',' + longitude);
        }
      })
    }
  },
  qqmap: function (location){
    const that = this;
    qqmapsdk.reverseGeocoder({
      location: location,
      success:function(res){
        that.setData({
          address: res.result.address
        })
      },
      fail:function(error){
        console.error(error);
      }
    })
  },
  bindGetUserInfo: function (e) {
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(a) {
              var str = JSON.parse(a.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: res.data.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            console.log(res)
                            that.setData({
                              modalName: null
                            })
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                        app.util.request({
                          url: 'Index/Index/ButtonNavList',
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            var data = res.data;
                            for (var i = 0; i < data.length; i++) {
                              data[i]['index'] = i;
                              data[i]['pagePath'] = data[i]['path'];
                              data[i]['text'] = data[i]['name'];
                              data[i]['iconPath'] = data[i]['before_image'];
                              data[i]['selectedIconPath'] = data[i]['after_image'];
                              data[i]['selected'] = false;
                            }
                            app.util.request({
                              url: 'Index/Index/getSumMessage',
                              data: {
                                openid: app.globalData.userInfo.openid,
                              },
                              header: {
                                'content-type': 'application/json'
                              },
                              success(res) {
                                data[2]['dott'] = res.data;
                                that.setData({
                                  dottot: data[2]['dott']
                                })
                              }
                            })
                            data[0]['selected'] = true;
                            app.globalData.tabbar.list = res.data;
                            app.setchangeTabBar();
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  takeOrders: function (e) {
    const that = this;
    const index = e.target.dataset.index;
    const data = that.data.template_zhuan;
    const id = data[index]['id'];
    data[index]['order'] = '1';
    that.setData({
      template_zhuan: data
    })
    app.util.request({
      url: 'Index/Index/takeOrders',
      data: {
        openid: app.globalData.userInfo.openid,
        id:id
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if(res.code === 1){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          return false;
        }else{
          wx.showToast({
            title: '接单成功',
            icon: 'success',
            duration: 2000
          });
          that.onShow();
        }
      }
    })
  },
  onShow:function(res){
    const that = this;
    // 检验是否授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            login: true,
            modalName: 'Image'
          })
        } else {
          that.setData({
            login: false
          })
        }
      }
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              var str = JSON.parse(res.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        that.setData({
                          userInfo: res.data
                        })
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: app.globalData.userInfo.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                        app.util.request({
                          url: 'Index/Index/getOrderList', // 仅为示例，并非真实的接口地址
                          header: {
                            'content-type': 'application/json' // 默认值
                          },
                          success(res) {
                            var data = res.data;
                            for (var i = 0; i < data.length; i++) {
                              data[i]['head'] = '../../../resource/images/index_file.png';
                              data[i]['name'] = data[i]['order_details']['destination_name'];
                              data[i]['time'] = data[i]['order_details']['create_time'];
                              data[i]['reward'] = parseFloat(data[i]['order_details']['cost']);
                              data[i]['quFileImg'] = '../../../resource/images/index_file.png';
                              data[i]['quFile'] = data[i]['order_details']['content'];
                              data[i]['quTimeImg'] = '../../../resource/images/index_time.png';
                              data[i]['quTime'] = data[i]['order_details']['depart_time'];
                              data[i]['quQiImg'] = '../../../resource/images/index_qi.png';
                              data[i]['quQi'] = data[i]['order_details']['origin'];
                              data[i]['quZhongImg'] = '../../../resource/images/index_zhong.png';
                              data[i]['quZhong'] = data[i]['order_details']['terminus'];
                              data[i]['qiangDan'] = '抢单';
                              data[i]['yiGet'] = '已接单';
                              if (data[i]['order_state'] !== '已付款'){
                                data[i]['order'] = 1;
                              }
                              if (data[i]['order_state'] === '已付款'){
                                data[i]['order'] = 0;
                              }
                            }
                            that.setData({
                              template_zhuan: data
                            })
                          }
                        })
                        app.util.request({
                          url: 'Index/Index/ButtonNavList',
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            var data = res.data;
                            for (var i = 0; i < data.length; i++) {
                              data[i]['index'] = i;
                              data[i]['pagePath'] = data[i]['path'];
                              data[i]['text'] = data[i]['name'];
                              data[i]['iconPath'] = data[i]['before_image'];
                              data[i]['selectedIconPath'] = data[i]['after_image'];
                              data[i]['selected'] = false;
                            }
                            var sum = 0;
                            app.util.request({
                              url: 'Index/Index/getSumMessage',
                              data: {
                                openid: app.globalData.userInfo.openid,
                              },
                              header: {
                                'content-type': 'application/json'
                              },
                              success(res) {
                                data[2]['dott'] = res.data;
                                data[0]['selected'] = true;
                                app.globalData.tabbar.list = data;
                                app.setchangeTabBar();
                              }
                            })
                            data[0]['selected'] = true;
                            app.globalData.tabbar.list = res.data;
                            app.setchangeTabBar();
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        } else {
          // wx.showToast({
          //   title: '请点击头像获取授权信息',
          //   icon: 'none',
          //   duration: 2000
          // });
          // return false;
        }
      }
    })
    const value = wx.getStorageSync('localhost');
    if(value){
      that.qqmap(value.latitude + ',' + value.longitude);
    }else{
      wx.getLocation({
        type: 'wgs84',
        success(res){
          const latitude = res.latitude
          const longitude = res.longitude
          that.qqmap(latitude + ',' + longitude);
        }
      })
    }
  },
  hunter:function(){
    wx.navigateTo({
      url: '../hunter/hunter'
    })
  },
  reward:function(){
    wx.navigateTo({
      url: '../reward/reward'
    })
  },
  DotStyle(e){
    this.setData({
      DotStyle: e.detail.value
    })
  },
  cardSwiper(e){
    this.setData({
      cardCur: e.detail.current
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
});